package com.lucky.common.interceptor;

import com.lucky.common.enums.AppConst;
import com.lucky.common.pojo.po.UserPO;
import com.lucky.common.service.ParamService;
import com.lucky.common.service.UserService;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class SessionInterceptor implements HandlerInterceptor {

	public Logger log = LoggerFactory.getLogger(SessionInterceptor.class);
	
	@Resource
	UserService userService;
	
	@Resource
	ParamService paramService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        log.info("SessionInterceptor: uri={}", request.getRequestURI());
		HttpSession session = request.getSession();
		UserPO user = (UserPO) session.getAttribute(AppConst.userPO);

		if(user==null){
			response.sendRedirect(AppConst.not_login);
			return false;
		}else {
			log.info("SessionInterceptor: user={}", user);
		}
		return true;

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
}
